/**
 * 
 */
package com.vector.ding.safe;

import java.util.List;

import com.vector.ding.commons.util.collection.CollectionUtil;
import com.vector.ding.commons.util.dao.MapperUtil;
import com.vector.ding.dao.mybatis.mapper.UserMapper;
import com.vector.ding.dao.mybatis.model.User;
import com.vector.ding.dao.mybatis.model.UserExample;

/**
 * 安全相关服务Service
 * @author vector
 *
 */
public class SecurityService {
	
	private static UserMapper userMapper = MapperUtil.getMapper(UserMapper.class);
	
	/**
	 * 用户是否存在
	 * @param userName	用户名
	 * @param pwMd5		密码的MD5值
	 * @return
	 */
	public static boolean hasUser(String userName, String pwMd5) {
		UserExample example = new UserExample();
		example.createCriteria().andNameEqualTo(userName).andPasswordEqualTo(pwMd5);
		int count = userMapper.countByExample(example);
		
		return count == 1;
	}
	
	/**
	 * get user form loginname
	 * @param userName
	 * @param pwMd5
	 * @return
	 */
	public static User getUser(String userName, String pwMd5) {
		UserExample example = new UserExample();
		example.createCriteria().andNameEqualTo(userName).andPasswordEqualTo(pwMd5);
		List<User> users = userMapper.selectByExample(example);
		if (CollectionUtil.isEmpty(users))
			return null;
		
		return users.get(0);
	}
	
	/**
	 * get user(table pojo) with loginName
	 * @param userName
	 * @return
	 */
	public static User getUser(String loginName) {
		UserExample example = new UserExample();
		example.createCriteria().andNameEqualTo(loginName);
		List<User> users = userMapper.selectByExample(example);
		if (CollectionUtil.isEmpty(users))
			return null;
		
		return users.get(0);
	}
	
}
